﻿using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// StartWorkspaceSelector.xaml 的交互逻辑
    /// </summary>
    public partial class StartWorkspaceSelector : MetroWindow
    {
        public StartWorkspaceSelector()
        {
            InitializeComponent();

            //载入之前指定的各个历史工作区目录
            if (File.Exists(Globals.PathOfHistoryWorkspaceFileFullName))
            {
                string[] workspaces = File.ReadAllLines(Globals.PathOfHistoryWorkspaceFileFullName);
                if (workspaces.Length > 0)
                {
                    for (int i = 0; i < workspaces.Length; i++)
                    {
                        if (Directory.Exists(workspaces[i]) == false) continue;

                        var rw = new RecentDirectoryListBoxItem(workspaces[i])
                        {
                            ToolTip = "双击设置为当前工作区目录",
                        };
                        rw.MouseDoubleClick += rw_MouseDoubleClick;
                        lsWorkspaces.Items.Add(rw);
                    }
                }
            }

            if (lsWorkspaces.Items.Count > 0)
            {
                lsWorkspaces.SelectedIndex = 0;
                lsWorkspaces.Focus();
            }
            else
            {
                btnOK.Focus();
            }

            this.Loaded += StartWorkspaceSelector_Loaded;
        }

        private void StartWorkspaceSelector_Loaded(object sender, RoutedEventArgs e)
        {
            if (lsWorkspaces.Items.Count > 0)
            {
                lsWorkspaces.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 双击“最近工作区列表”中的某个条目，将工作区切换到该条目记录的目录。
        /// </summary>
        void rw_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this.Close();

            (App.Current.MainWindow as MainWindow).ChangeWorkspace((sender as RecentDirectoryListBoxItem).DirectoryPath);
        }

        public string WorkspacesFullPath
        {
            get
            {
                var item = lsWorkspaces.SelectedItem as RecentDirectoryListBoxItem;
                if (item == null) return "";

                return item.DirectoryPath;
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (Directory.Exists(this.WorkspacesFullPath))
            {
                bdMask.Visibility = Visibility.Visible;
                this.Close();

                App.ConfigManager.Set("Workspace", this.WorkspacesFullPath);
                Globals.MainWindow.ChangeWorkspace(this.WorkspacesFullPath);

            }
            else
            {
                prompt.Text = "目录不存在。";
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            App.Current.Shutdown();
        }

        private void MetroWindow_Closed(object sender, EventArgs e)
        {
            Globals.MainWindow.StartWorkspaceSelector = null;
            if (Owner != null) Owner.Activate();
        }
    }
}
